#include <iostream>
#include <allegro.h>
#include "spaceShip.h"

#define STARTX 20
#define STARTY 20
#define BITMAP1 "enterprise.bmp"
#define BITMAP2 "crashprise.bmp"
#define BITMAPSIZE_X 120
#define BITMAPSIZE_Y 76
#define MOONBASESIZE 200
#define A 1.6
#define THRUST_X 5.0f
#define THRUST_Y 5.0f
#define GASAMOUNT 500
#define NUM_POINTS 12
#define MAXLANDINGSPEED 10
#define GOODLANDINGSPEED 5
#define EXCELLENTLANDINGSPEED 2


int lastx=0;
int lasty=0;
int level=1;
bool end=false;
bool crash=false;
int points[NUM_POINTS];
spaceShip *ship;
//Points for collision detection in the Bitmap
int bitmapPoints[10]={8,26,113,26,62,69,51,65,70,64}; 

void drawSprite(BITMAP *Sprite,double *x, double *y)
{
	acquire_screen();
	draw_sprite( screen, Sprite, *x, *y);
	release_screen();	
}

void drawMoonBase()
{
    acquire_screen();
	line( screen, SCREEN_W-MOONBASESIZE,SCREEN_H-2 , SCREEN_W, SCREEN_H-2, makecol( 255, 0, 0));
	release_screen();
}

void randomMountain()
{
	points[0]=0;
	points[1]=SCREEN_H;
	points[NUM_POINTS-2]=SCREEN_W-MOONBASESIZE;
	points[NUM_POINTS-1]=SCREEN_H;
	int x=(SCREEN_W-MOONBASESIZE)/5;
	for(int i=2;i<NUM_POINTS-2;i+=2)
	{
		points[i]=x;
		points[i+1]=SCREEN_H-std::rand()%(SCREEN_H-100);
		x+=(SCREEN_W-MOONBASESIZE)/(NUM_POINTS/2-1);
	}
}
void drawMountains()
{	
	polygon(screen, NUM_POINTS/2, points, makecol(55, 55, 55));
}

void drawGas(int *x)
{
	int points[12] = { 50, 50,   100, 100,  100, 150,50, 200,  0,   150,  0,   100 };
	rectfill(screen,SCREEN_W-10-GASAMOUNT,5,SCREEN_W-10-GASAMOUNT+*x,15,makecol(255,0,0));
	rectfill(screen,SCREEN_W-10-GASAMOUNT+*x,5,SCREEN_W-10,15,makecol(0,0,0));
}

void drawSpeed(double* speed)
{
	rectfill(screen,0,15,130,35,makecol(0,0,0));
	textprintf_centre_ex(screen, font, 70, 25, makecol(255, 0, 0), -1,"Speed:  %.1f m/s",*speed);
	textprintf_centre_ex(screen, font, 70, 15, makecol(255, 0, 0), -1,"Level:  %d",level);
}

void drawText(char* text,int y)
{
	textprintf_centre_ex(screen, font, SCREEN_W/2-50, y, makecol(255, 0, 0), -1,text);
}

void newGame()
{
	end=false;
	level++;
	ship->setValues(STARTX,STARTY,A,GASAMOUNT);
	clear_bitmap(screen);
	randomMountain();
	drawMoonBase();
	drawMountains();
}

void collision(int x,int y)
{
	for(int i=0;i<NUM_POINTS;i+=2)
	{
		if(x>=points[i] && x<=points[i+2])
		{
			//Interpolation
			float distance=(float)points[i+2]-(float)points[i];
			float b=((float)points[i+2]-(float)x);
			float a=(float)x-(float)points[i];
			float y_b=(a/distance)*(float)points[i+3];
			float y_a=(b/distance)*(float)points[i+1];

			if(y>=y_a+y_b)
			{
 				crash=true;
				end=true;
			}
		}
	}
	if(x>SCREEN_W && y >SCREEN_H || x<0 && y >SCREEN_H)
	{	
		crash=true;
		end=true;
	}
}

bool checkLanding(double* x,double* y)
{
	if(*y >=SCREEN_H-70 && *x+BITMAPSIZE_X/2<=SCREEN_W && *x+BITMAPSIZE_X/2>=SCREEN_W-MOONBASESIZE)
	{
		end=true;
		return true;
	}
	return false;
}

int main()
{
	srand((unsigned)time(0));
	BITMAP *enterprise;
	BITMAP *crashprise;
	allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 1024,600, 0, 0);
	
	randomMountain();
	enterprise = load_bitmap( BITMAP1, NULL);
	crashprise = load_bitmap( BITMAP2, NULL);

	ship = new spaceShip();
	ship->setValues(STARTX,STARTY,A,GASAMOUNT);
	drawMoonBase();
	drawMountains();

	while( !key[KEY_ESC] ){


		while(!end)
		{
			rest(10);
			
			if(key[KEY_UP])
			{
				ship->moveUp(THRUST_Y);
			}

			else if(key[KEY_RIGHT])
			{
				ship->moveSidewards(THRUST_X);
			}
			else if(key[KEY_LEFT])
			{
				ship->moveSidewards(-THRUST_X);
			}

			else
			{
				ship->setThrust();
				ship->claculate(0.1);
			}

			drawSprite(enterprise,ship->getX(),ship->getY());
			drawGas(ship->getGas());
			drawSpeed(ship->getSpeed());
			drawMoonBase();
			drawMountains();
			for(int i=0;i<5;i+=2)
			collision((int)*ship->getX()+bitmapPoints[i],(int)*ship->getY()+bitmapPoints[i+1]);
			
				if(!crash && checkLanding(ship->getX(),ship->getY()))
				{
					if(*ship->getSpeed() < EXCELLENTLANDINGSPEED)
						{
							drawMoonBase();
							drawText("GODLIKE!!!!!!!!!",SCREEN_H/2);
						}
					else if(*ship->getSpeed() < GOODLANDINGSPEED)
						{
							drawMoonBase();
							drawText("EXCELLENT LANDING",SCREEN_H/2);
						}
					else if(*ship->getSpeed() < MAXLANDINGSPEED)
						{
							drawMoonBase();
							drawText("GOOD LANDING",SCREEN_H/2);
						}
					else if(*ship->getSpeed() >= MAXLANDINGSPEED)
						{
							drawSprite(crashprise,ship->getX(),ship->getY());
							drawMoonBase();
							drawText("CRASH!!!!  ENTERPRISE KAPUTT!!",SCREEN_H/2);
							drawText("ENTER -> NEW GAME",SCREEN_H/2+60);
							level=0;
						}
					if(*ship->getSpeed() < MAXLANDINGSPEED)
					{
							drawText("ENTER -> NEXT LEVEL",SCREEN_H/2+60);
					}
				}
		}


		if(crash)
		{
			drawSprite(crashprise,ship->getX(),ship->getY());
			drawMountains();
			drawText("CRASH!!!!  YOU KILLED JAMES T. KIRK AND COMMANDER SPOCK !!!!",SCREEN_H/2);
			drawText("ENTER -> NEW GAME",SCREEN_H/2+60);
			level=0;
			crash=false;
		}
		
					

		drawText("   ESC   -> END  ",SCREEN_H/2 +30);

		if( key[KEY_ENTER])
			newGame();


	}
	destroy_bitmap(enterprise);
	destroy_bitmap(crashprise);
	return 0;
}
END_OF_MAIN();